package com.example.xd.tuiliu_test;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @auther XuDan on 2020/5/11.
 */
public class NetTest {
    public static void main(String[] args) throws Exception {
        //使用getLocalHost方法创建InetAddress对象  本机
        InetAddress addr = InetAddress.getLocalHost();
        System.out.println(addr.getHostAddress());
        System.out.println(addr.getHostName());

        //根据域名得到InetAddress对象
        InetAddress addr1 = InetAddress.getByName("www.baidu.com");
        System.out.println(addr1.getHostAddress());
        System.out.println(addr1.getHostName());

        //根据IP得到InetAddress对象
        InetAddress addr2 = InetAddress.getByName("10.1.120.224");
        System.out.println(addr2.getHostAddress());
        System.out.println(addr2.getHostName());

        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8888);
        InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 9999);
        System.out.println(socketAddress.getHostName());
        System.out.println(socketAddress2.getAddress());
    }
}
